package com.bookquery.servlet;

import com.alibaba.fastjson.JSON;
import com.bookquery.entity.BookQuery;
import com.bookquery.service.BookQueryService;
import com.bookquery.service.BookQueryServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/Home"}, name = "QueryAllServlet")
public class QueryAllServlet extends HttpServlet {
    private BookQueryService bookQueryService;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            List<BookQuery> infos =bookQueryService.getAll();
        String infoJson = JSON.toJSONString(infos);
        response.getWriter().print(infoJson);
        response.getWriter().close();
        response.getWriter().flush();

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        this.doGet(request, response);
    }

    @Override
    public void init() {
        bookQueryService = new BookQueryServiceImpl();
    }
}
